package com.xuan.system.manager;


import com.xuan.common.core.constants.CacheConstants;
import com.xuan.common.core.constants.Constants;
import com.xuan.common.redis.service.RedisService;
import com.xuan.system.domain.contest.entity.Contest;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.time.ZoneId;

@Component
public class ContestCacheManager {

    @Resource
    private RedisService redisService;


    public void addCache(Contest contest) {
        //  将信息存储到 redis(未完赛列表c:t:l    竞赛基本信息c:d:)
        double endEpochSecond = contest.getStartTime().atZone(ZoneId.of(Constants.ZONE_ID)).toEpochSecond();
        redisService.zAdd(getContestZSetKey(), contest.getContestId(), endEpochSecond);
        redisService.setCacheObject(getDetailKey(contest), contest);
    }

    public void deleteCache(Contest contest) {
        //  将信息从 redis 中删除(未完赛列表c:t:l    竞赛基本信息c:d:     竞赛-题目信息c:q:l: )
        redisService.removeForZSet(getContestZSetKey(), contest.getContestId());
        redisService.deleteObject(getDetailKey(contest));
        redisService.deleteObject(CacheConstants.CONTEST_QUESTION_LIST_KEY + contest.getContestId());
    }


    private String getDetailKey(Contest contest) {
        return CacheConstants.CONTEST_DETAIL_KEY + contest.getContestId();
    }

    private String getContestZSetKey() {
        return CacheConstants.CONTEST_UNFINISHED_ZSET_KEY;
    }
}
